<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script>
        /*获得1970.1.1距今为止过了多少毫秒*/
        var date1 = new Date();
        var date2 = +new Date();
        console.log(date1.valueOf()); 
        console.log(date1.getTime());
        console.log(date2);//最常用写法
        console.log(Date.now());

        /*倒计时*/
        //使用输入未来时间和现在时间毫秒时间戳来对减，所得毫秒再转换为时间即可
        function countDown(time) {
            var nowTime = +new Date();//返回的是当前时间总的毫秒数
            var inputTime = +new Date(time);//返回的是用户输入时间总的毫秒数
            var times = (inputTime-nowTime)/1000;//time是剩余时间总的秒数
            d = d<10?'0'+d:d;
            var d = parseInt(times/60/60/24);//计算天数
            h = h<10?'0'+h:h;
            var h = parseInt(times/60/60%24);//计算小时
            m = m<10?'0'+m:m;
            var m = parseInt(times/60%60);//计算分数
            s = s<10?'0'+s:s;
            var s = parseInt(times%60);//计算秒数
            return d+'天'+h+'时'+m+'分'+s+'秒';
        }
        console.log(countDown('2022-7-20 18:00:00'));
    </script>
</body>
</html>